package com.hktf.moedokilock.fragment;


import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hktf.corelib.constant.ActiveType;
import com.hktf.corelib.constant.KEYS;
import com.hktf.corelib.utils.ButtonUtil;
import com.hktf.corelib.utils.LogMds;
import com.hktf.corelib.utils.MMKVUtils;
import com.hktf.moedokilock.R;
import com.hktf.moedokilock.activity.PreviewWallpaperActivity;
import com.hktf.moedokilock.adapter.WallpaperItemAdapter;
import com.hktf.moedokilock.bean.WallpaperItem;
import com.hktf.uilib.app.BaseActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * date: 2025/3/15 17:19.
 *
 * @auther: cdz
 * @function:
 */
public class MainFragment0 extends BaseFragment implements View.OnClickListener, BaseQuickAdapter.OnItemClickListener {
    private View view;
    private TextView tvMore;
    private RecyclerView recyclerView;
    private CheckBox cb_open;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // TODO: 2022/12/14 此方法晚于 onCreateView执行
        EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main0, container, false);
        BaseActivity activity = (BaseActivity) getActivity();
        activity.setRootViewPaddingTop(view);
        initView(view);
        initData();

        return view;
    }

    private WallpaperItemAdapter mAdapter;


    private void initData() {

        recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));


        mAdapter = new WallpaperItemAdapter(new ArrayList<>());
        recyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                int id = view.getId();
                if (id == R.id.fl_check) {
                    if (checkdIndex == position) {
                        return;
                    }
                    WallpaperItem item = mAdapter.getItem(position);
                    item.setChecked(true);
                    if (checkdIndex != -1) {
                        mAdapter.getItem(checkdIndex).setChecked(false);
                    }
                    checkdIndex = position;
                    mAdapter.notifyDataSetChanged();
                    BaseActivity baseActivity = (BaseActivity) getActivity();
                    baseActivity.saveCheckLock(item.getResId());

                } else if (id == R.id.fl_delete) {
                    // TODO: 2025/3/20 删除逻辑 

                }


            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WallpaperItem item = mAdapter.getItem(position);
                Intent intent = new Intent(getActivity(), PreviewWallpaperActivity.class);
                intent.putExtra("resId", item.getResId());
                startActivity(intent);
            }
        });
        refreshData();


    }

    private int checkdIndex = -1;


    @Override
    public void onClick(View v) {
        //                if(true){
        //                    DlgUtils.showQrcode(getActivity(), getString(com.hktf.uilib.R.string.c90_nopay), "htttp:baidu",null);
        //                    return;
        //                }
        int id = v.getId();
        if (ButtonUtil.isFastDoubleClick(id, 2000)) {
            LogMds.e("cdz", "hidden 双击了.....position=" + id);
            return;
        }
        if (id == R.id.tv_title) {
            //签到
            //            ToastUtils.showToast(getContext(),"签到");
            gotoDiagnose();
        }
    }


    private AnimationDrawable mAnimationDrawable;


    @Override
    public void onDestroy() {
        super.onDestroy();
    }


    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }


    private void refreshUi(int position) {

    }

    @Override
    public void onResume() {
        super.onResume();
        refreshData();
    }

    private boolean isBindStation;

    private void refreshData() {
        int[] checkedResIds = new int[]{
                R.drawable.lock1, R.drawable.lock2, R.drawable.lock3, R.drawable.lock4,
                R.drawable.lock5, R.drawable.lock6, R.drawable.lock7, R.drawable.lock8
                ,R.drawable.lock9
        };
        List<WallpaperItem> list = new ArrayList<>();
        int lockResId = MMKVUtils.getInteger(KEYS.LOCK_RESID);
        for (int checkedResId : checkedResIds) {
            WallpaperItem item = new WallpaperItem();
            item.setResId(checkedResId);
            if (lockResId == checkedResId) {
                item.setChecked(true);
            } else {
                item.setChecked(false);
            }
            list.add(item);
        }
        mAdapter.setNewData(list);
        mAdapter.notifyDataSetChanged();
    }


    public void gotoDiagnose() {
    }


    /**
     * 激活连接蓝牙回调
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ActiveType activeType) {

    }

    private void initView(View view) {
        tvMore = (TextView) view.findViewById(R.id.tv_more);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        cb_open = view.findViewById(R.id.cb_open);


        Boolean aBoolean = MMKVUtils.getBoolean(KEYS.IS_LOCK, false);
        if (aBoolean) {
            BaseActivity baseActivity = (BaseActivity) getActivity();
//            baseActivity.checkFloatPermission();
        }
        cb_open.setChecked(aBoolean);
        cb_open.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                MMKVUtils.putBoolean(KEYS.IS_LOCK, isChecked);
                BaseActivity baseActivity = (BaseActivity) getActivity();
                if (isChecked) {
                    baseActivity.checkFloatPermission();
                } else {

                }
            }
        });

    }
}
